// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-02-13

package conf

import (
	"regexp"
	"time"
)

// ErpClient
// 与ERP进销存系统进行API数据交互.
type ErpClient struct {
	// Authorization
	// 鉴权参数.
	//
	// 适用于 ERP/V8 (https://www.nodcloud.com/product/erp)并开启开启API接口
	//
	// 1. 进入 系统 > 系统参数 > 功能参数 > API接口 页面, 打开API接口.
	// 2. 进入 系统 > 用户管理 页面，找到指定用户并点击 API 链接.
	// 3. 打开 启用状态, 然后复制下方的 API 密钥.
	Authorization string `yaml:"authorization"`

	// Host
	// 服务地址.
	//
	// 例如: https://erp.nodcloud.com/
	Host string `yaml:"host"`

	// Timeout
	// 超时时长.
	//
	//  - 单位: 秒
	//  - 默认: 10
	Timeout int `yaml:"timeout"`

	// 动态计算

	// TimeoutDuration
	// 超时时长.
	TimeoutDuration time.Duration `yaml:"-"`
}

func (o *ErpClient) Defaulter() {
	if o.Host != "" {
		o.Host = regexp.MustCompile(`/+$`).ReplaceAllString(o.Host, ``)
	}

	if o.Timeout <= 0 {
		o.Timeout = 10
	}
	o.TimeoutDuration = time.Duration(o.Timeout) * time.Second
}
